define(window.backboneCommon.getViewDefine("agent.detail"), function ($,require,Collection,tpl) {
    var backboneView= Backbone.View.extend({
        events: {
            "click #btn_submitSave": "saveSubmit"
        },
        initialize: function () {
            this.listenTo(this.model, 'bind:Info', this.bindInfo);
            this.listenTo(this.model, 'bind:RebateDisable', this._bindRebateDisable);
            this.listenTo(this.model, 'save:Info', this.saveInfoCallback);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(id){
            this.elementModel={};
            this.extObject={};
            this.pageData={initPage:false,agentId:id};
            this.elementModel.form=$("#form_detail");
            this.pageData.listStatus=this.model.get("defListStatus");
            this._initPagePower();
        },
        _initPagePower:function(){
            var self=this;
            $.ExtAssist.rolePower.operatePage("select",["edit"],function(json){
                self.pageData.power={
                    isOnlyView:json["select"],
                    isEdit:json["edit"]
                };
                self._initBodyMain();
            });
        },
        _initBodyMain:function(){
            this._bindStatus();
            this.collection.getInfo(this.pageData.agentId);
            $.ExtAssist.urlParam.setUrl();
        },
        _bindStatus:function () {
            this.extObject.status=$("#panel_status").ExtRadioCheckbox({type: "radio",attrName:"status",defaultInfo:{width:75},
                data:this.pageData.listStatus
            });
        },
        bindInfo:function () {
            var self=this;
            var result=this.model.get("dataInfo");
            self.pageData.isAdd=result.isAdd;
            $(".modal-title").text($.ExtCommon.getStringConnect("{0}代理商",self.pageData.isAdd?"新建":"编辑"));
            self.extObject.form=self.elementModel.form.ExtFormValue({isRunBind:false,
                initCallback:function(dataJson){
                    self.extObject.status.setValue(dataJson.status.toString());
                    self.collection.getCustomerNum(dataJson.id);
                    self.elementModel.form.ExtParsley("reset");
                    self._modalShow();
                }
            });
            self.extObject.form.cleanValue();
            self.extObject.form.bindValueData(result.res.data);
            $.ExtNumeric.initAll();
        },
        _bindRebateDisable:function () {
            var self=this;
            var result=this.model.get("dataInfo2");
            self.pageData.customerNumber=result.res.data.number;
            $("#txt_commission").prop("disabled", self.pageData.customerNumber>0);
        },
        _modalShow:function () {
            $("#modal_detail").modal("show");
            $.ExtAssist.modal.computeTop("#modal_detail");
        },
        _modalHide:function () {
            $("#modal_detail").modal("hide")
        },
        _checkForm:function () {
            return this.elementModel.form.ExtParsley("validate");
        },
        saveSubmit:function () {
            if(this._checkForm()) {
                var infoOriginal = this.elementModel.form.ExtFormValue("getOriginalData");
                var ajaxData = this.elementModel.form.ExtFormValue("getValues");
                if (infoOriginal.id) {ajaxData.id = infoOriginal.id}
                this.collection.saveInfo(ajaxData);
                this._modalHide();
            }
        },
        saveInfoCallback:function () {
            var self=this;
            var result=this.model.get("dataSave");
            if (result.res.success) {
                $.ExtNotify.success("保存成功!");
                self.collection.getPageList({offset:0,limit:10})
            }else{
                self._modalShow();
                $.ExtNotify.error(result.res.msg);
            }
        }
    });
    var viewModel=function(options){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el:options.pElement,collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        init:function (param) {
            if(param.isAdd){
                this.view.initMain(null);
            }else{
                this.view.initMain(param.id);
            }
        }
    };
    return viewModel;
});